//
//  ZMYMyBookCommentTableViewController.m
//  奇阅
//
//  Created by 奇阅科技 on 14-11-3.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "ZMYMyBookCommentTableViewController.h"
#import "Config.h"
#import "ZMYMyBookCommentTableViewCell.h"
#import "ZMYThemeCommentTableViewController.h"
#import "UIScrollView+VORefresh.h"
#import "ZMYBookInfoViewController.h"
@interface ZMYMyBookCommentTableViewController ()<ZMYMyBookCommentTableViewCellDelegate>
{
    NSMutableArray *dataArray;
    BOOL isHasMore;
}
@end

@implementation ZMYMyBookCommentTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!self.isCatalogueView) {
        self.navigationController.navigationBarHidden = NO;
        [self setBarBackgroundImage:@"navBackground"];
        [self setTitleForNavigation:@"书评"];
        [self setLeftBackButtonForNavigation];
    }
    if (self.isCatalogueView) {
        self.defaultImageView.image = [UIImage imageNamed:@"defaultComment"];
    }
    [self.tableView addTopRefreshWithTarget:self action:@selector(getNetNewDataArray)];
    [self.tableView addBottomRefreshWithTarget:self action:@selector(getMoreDataArray)];
    [self initDataArray];
}
-(void)initDataArray{
    if (self.isCatalogueView) {
        dataArray = [NSMutableArray arrayWithArray:[[ZMYLKDB getLKDBHelper] getMyCommentBook:self.book Page:1]];
    } else {
        dataArray = [NSMutableArray arrayWithArray:[[ZMYLKDB getLKDBHelper] getMyCommentPage:1]];
    }
    
    [self.tableView reloadData];
    [self getNetNewDataArray];
}
-(void)getNetNewDataArray{
    [self.navigationController setSGProgressPercentage:80 andTintColor:YellowColor];
    //getMyThemeComment:(Book *)book
    if (self.isCatalogueView) {
        [[ZMYNetWorkAPI default]getMyThemeComment:self.book Page:1 Response:^(NSDictionary *resultDic) {
            [self.navigationController setSGProgressPercentage:100];
            NSArray *commentList = [resultDic objectForKey:@"commentList"];
            if (commentList.count>0) {
                dataArray = [NSMutableArray arrayWithArray:[[ZMYLKDB getLKDBHelper] getMyCommentBook:self.book Page:1]];
                [self.tableView reloadData];
            }
            NSInteger count = [[resultDic objectForKey:@"count"] integerValue];
            if (dataArray.count<count) {
                isHasMore = YES;
            }else{
                isHasMore = NO;
                [self.tableView removeBottomRefresh];
            }
            [self.tableView.topRefresh endRefreshing];
        } error:^(NSString *errorStr) {
            [self.tableView.topRefresh endRefreshing];
            [self.navigationController cancelSGProgress];
        }];
    } else {
        [[ZMYNetWorkAPI default]myCommentPage:1 Response:^(NSDictionary *resultDic) {
            [self.navigationController setSGProgressPercentage:100];
            NSArray *commentList = [resultDic objectForKey:@"commentList"];
            if (commentList.count>0) {
                dataArray = [NSMutableArray arrayWithArray:[[ZMYLKDB getLKDBHelper] getMyCommentPage:1]];
                [self.tableView reloadData];
            }
            NSInteger count = [[resultDic objectForKey:@"count"] integerValue];
            if (dataArray.count<count) {
                isHasMore = YES;
            }else{
                isHasMore = NO;
                [self.tableView removeBottomRefresh];
            }
            [self.tableView.topRefresh endRefreshing];
        } error:^(NSString *errorStr) {
            [self.tableView.topRefresh endRefreshing];
            [self.navigationController cancelSGProgress];
        }];
    }
    
}
-(void)getMoreDataArray{
    if (isHasMore) {
        //getMyThemeComment:
        if (self.isCatalogueView) {
            [[ZMYNetWorkAPI default]getMyThemeComment:self.book Page:dataArray.count/10+1 Response:^(NSDictionary *resultDic) {
                [self.tableView.bottomRefresh endRefreshing];
                [self.navigationController setSGProgressPercentage:100];
                NSArray *commentList = [resultDic objectForKey:@"commentList"];
                if (commentList.count>0) {
                    [dataArray addObjectsFromArray:[[ZMYLKDB getLKDBHelper] getMyCommentBook:self.book Page:dataArray.count/10+1]];
                    [self.tableView reloadData];
                }
                NSInteger count = [[resultDic objectForKey:@"count"] integerValue];
                if (dataArray.count<count) {
                    isHasMore = YES;
                }else{
                    isHasMore = NO;
                    [self.tableView removeBottomRefresh];
                }
                
            } error:^(NSString *errorStr) {
                [self.tableView.bottomRefresh endRefreshing];
                [AppDelegate showHUDAndHide:errorStr view:self.view];
            }];
        } else {
            [[ZMYNetWorkAPI default]myCommentPage:dataArray.count/10+1 Response:^(NSDictionary *resultDic) {
                [self.tableView.bottomRefresh endRefreshing];
                [self.navigationController setSGProgressPercentage:100];
                NSArray *commentList = [resultDic objectForKey:@"commentList"];
                if (commentList.count>0) {
                    [dataArray addObjectsFromArray:[[ZMYLKDB getLKDBHelper] getMyCommentPage:dataArray.count/10+1]];
                    [self.tableView reloadData];
                }
                NSInteger count = [[resultDic objectForKey:@"count"] integerValue];
                if (dataArray.count<count) {
                    isHasMore = YES;
                }else{
                    isHasMore = NO;
                    [self.tableView removeBottomRefresh];
                }
                
            } error:^(NSString *errorStr) {
                [self.tableView.bottomRefresh endRefreshing];
                [AppDelegate showHUDAndHide:errorStr view:self.view];
            }];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (dataArray.count<1) {
        self.defaultImageView.hidden = NO;
    } else {
        self.defaultImageView.hidden = YES;
    }
    return dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZMYMyBookCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZMYMyBookCommentTableViewCell" forIndexPath:indexPath];
    UserAccount *user = [UserAccount currentAccount];
    [cell.headImageView setImageWithURL:[NSURL URLWithString:user.headimg] placeholderImage:[UIImage imageNamed:@"touxiang"]];
    ThemeComment *comment = dataArray[indexPath.row];
    cell.nickNameLabel.text = user.nickname.length>1?user.nickname:user.username;
    cell.timeLabel.text = [NSString intDateToStrFromNSTimeInterval:comment.time];
    cell.commentLabel.text = comment.contents;
    [cell.bookCoverImageView setImageWithURL:[NSURL URLWithString:comment.book.imagefname] placeholderImage:[UIImage imageNamed:@"touxiang"]];
    cell.bookTitleLabel.text = comment.book.title;
    cell.bookAuthorLabel.text = comment.book.author;
    cell.praiseNumLabel.text = comment.point_like;
    cell.commentNumLabel.text = comment.reply_num;
    LikeComment *like = [LikeComment new];
    like.articleid = comment.articleid;
    like.commentid = comment.commentid;
    if (!user) {
        like.uid = @"love";
    } else {
        like.uid = user.uid;
    }
    if ([[ZMYLKDB getLKDBHelper]getIsLike:like]) {
        cell.likeImageView.image = [UIImage imageNamed:@"mypraise"];
        cell.praiseNumLabel.textColor = [UIColor whiteColor];
    } else {
        cell.likeImageView.image = [UIImage imageNamed:@"mynopraise"];
        cell.praiseNumLabel.textColor = RGB_COLOR(160, 160, 160);
    }
    cell.comment = comment;
    cell.delegate = self;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ThemeComment *comment = dataArray[indexPath.row];
    ZMYThemeCommentTableViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"ZMYThemeCommentTableViewController"];
    view.themeComment = comment;
    [self.navigationController pushViewController:view animated:YES];
}
-(void)bookInfo:(ThemeComment *)comment{
    ZMYBookInfoViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"ZMYBookInfoViewController"];
    view.book = comment.book;
    [self.navigationController pushViewController:view animated:YES];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - THSegmentedPageViewControllerDelegate
- (NSString *)viewControllerTitle
{
    return self.titleStr;
}
@end
